const _ = require('lodash');
const fs=require('fs');
const userItemModel = require('../../app/models/userInfo.json');

module.exports = {
    getByUserId : getByUserId,
    add: add
}

function getByUserId(userInfoId, callback) {
    let result = null;
    _.forEach(userItemModel, (item) => {
        if (_.isEqual(userInfoId, item.userInfoId)) {
            result = item;
        }
    })
    callback(null, result);
}

function add(user, callback) {

    userItemModel.push(user);
    var jsonString = JSON.stringify(userItemModel, null, 4);
    fs.writeFile("./app/models/userInfo.json", jsonString, (err) => {
        if (err) throw err;
        console.log('add success!');
    });
    callback(null, true);
}